<template>
 <view class="normal-login-container">
	
    <view class="logo-content align-center justify-center flex">
      <image style="width: 100rpx;height: 100rpx;" :src="globalConfig.appInfo.logo" mode="widthFix">
      </image>
      <text class="title">大智移动端登录</text>
    </view>
    <view class="login-form-content">
      <view class="input-item flex align-center">
        <view class="iconfont icon-user icon"></view>
        <input v-model="loginForm.userName" class="input" type="text" placeholder="请输入账号" maxlength="30" />
      </view>
      <view class="input-item flex align-center">
        <view class="iconfont icon-password icon"></view>
        <input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
      </view>
      <view class="action-btn">
        <button @click="handleLogin()" class="login-btn cu-btn block bg-blue lg round">登录</button>
      </view>
      <view class="reg text-center" v-if="register">
        <text class="text-grey1">没有账号？</text>
        <text @click="handleUserRegister()" class="text-blue">立即注册</text>
      </view>
    </view>
     
  </view>
</template>

<script>
import { login, getInfo } from '@/api/login'
import {getLoginRequired} from '@/api/system/user'
import { toast} from '@/utils/common'
  export default {
    data() {
      return {
        codeUrl: "",
        captchaEnabled: true,
        // 用户注册开关
        globalConfig: getApp().globalData.config,
        loginForm: {
          userName: "master",
          password: "123456",
		  openId:null,
		  validateCode:null
        },
		 register: true,
      } 
    },
      created() {
    	this.getUrlCode()
		
    },
  
    methods: {
	 	//  截取url中的code方法
		   getUrlCode() {
			
		    var url = document.location.search
		    var theRequest = {}
		    if (url.indexOf('?') !== -1) {
		      var str = url.substring(1)
		      var strs = str.split('&')
		      for (var i = 0; i < strs.length; i++) {
		        theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1]
		      }
		    }
			  let code = theRequest.code
			 // let code= '091wwAGa1zB9WG0qoiHa1aSa9D1wwAGP'
			  // alert(code)
			  const ua = navigator.userAgent.toLowerCase()
			  if (ua.match(/MicroMessenger/i)) {
			    const isWeixin = ua.match(/MicroMessenger/i).some(item => item === 'micromessenger')
			    if (isWeixin){
					getLoginRequired({code:code}).then( res=>{
						this.loginForm.openId=res.msg
						if(res.flag){
						  this.loginSuccess()
						}
					})
				}
				
				}
			
		  },
		
      // 用户注册
      handleUserRegister() {
		  let openId = this.loginForm.openId
		  uni.navigateTo({
		         url: '/pages/register',
		         success: (respost) => {
		             // 通过eventChannel向被打开页面传送数据
		             respost.eventChannel.emit('acceptDataFromOpenerPage', {data:openId},
		  		   )}
		       });
		  
        // this.$tab.redirectTo('/pages/register')
      },
     
      // 登录方法
      async handleLogin() {
        if (this.loginForm.userName === "") {
          this.$modal.msgError("请输入您的账号")
        } else if (this.loginForm.password === "") {
          this.$modal.msgError("请输入您的密码")
        }else {
          this.$modal.loading("登录中，请耐心等待...")
          this.pwdLogin()
        }
      },
      // 密码登录
      async pwdLogin() {
		let userName = this.loginForm.userName.trim()
		let password = this.loginForm.password
		let openId = this.loginForm.openId
        login(userName, password,openId).then(res => {
		 if(res.flag){
           this.$modal.closeLoading()
           this.loginSuccess()
         }else{
         		this.$modal.closeLoading()
				toast(res.msg)
					
         		return
         }
		 
		 
        }).catch(() => {
   
        })
      },
      // 登录成功后，处理函数
      loginSuccess(result) {
      // 设置用户信息
		
        this.$store.dispatch('GetInfo').then(res => {
          this.$tab.reLaunch('/pages/work/index')
        })
      }
    }
 
  }
</script>

<style lang="scss">
 page {
    background-color: #55557f;
 	background-image: url(/static/images/dz.jpg);
 	background-size:100%;
 }

  .normal-login-container {
    // width: 100%;

    .logo-content {
      width: 100%;
      font-size: 21px;
      text-align: center;
      padding-top: 15%;

      image {
        border-radius: 4px;
      }

      .title {
        margin-left: 10px;
      }
    }

    .login-form-content {
      text-align: center;
      margin: 20px auto;
      margin-top: 15%;
      width: 80%;

      .input-item {
        margin: 20px auto;
        background-color: #f5f6f7;
        height: 45px;
        border-radius: 20px;

        .icon {
          font-size: 38rpx;
          margin-left: 10px;
          color: #999;
        }

        .input {
          width: 100%;
          font-size: 14px;
          line-height: 20px;
          text-align: left;
          padding-left: 15px;
        }

      }

      .login-btn {
        margin-top: 40px;
        height: 45px;
      }
      
      .reg {
        margin-top: 15px;
      }
      
      .xieyi {
        color: #333;
        margin-top: 20px;
      }
      
      .login-code {
        height: 38px;
        float: right;
      
        .login-code-img {
          height: 38px;
          position: absolute;
          margin-left: 10px;
          width: 200rpx;
        }
      }
    }
  }

</style>
